# -*- coding: utf-8 -*-
import wx
import wx.lib.scrolledpanel as scrolled
import urllib
import random
import re

class VideoPanel(wx.Panel):
	def __init__(self,parent,videocode,pos,size=(560,130)):

		# EDITABLE ATTRIBUTES
		# self.url=

		wx.Panel.__init__(self, parent, size=size, pos=(10,10+120*pos))
		self.SetBackgroundColour("#cecece")

		self.borderSizer = wx.BoxSizer(wx.HORIZONTAL)

		self.contentPanel = wx.Panel(self,size=(550,120), pos=(5,5))
		self.borderSizer.Add(self.contentPanel, 0, wx.ALL, border=5)

		self.contentSizer = wx.BoxSizer(wx.HORIZONTAL)

		self.tmpThumbnail = self.getThumbnailTmp(videocode)[0]
		jpg = wx.Image(self.tmpThumbnail,wx.BITMAP_TYPE_ANY).ConvertToBitmap()
		self.thumbnail = wx.StaticBitmap(self.contentPanel, -1, jpg, pos=(0,0), size=(jpg.GetWidth(), jpg.GetHeight()))
		self.contentSizer.Add(self.thumbnail,0,flag=wx.RIGHT,border=10)

		self.text = wx.TextCtrl(self.contentPanel,value="",style=wx.NO_BORDER|wx.TE_READONLY)
		self.text.SetBackgroundColour("#cecece")
		self.contentSizer.Add(self.text,0)

		self.contentPanel.SetSizer(self.contentSizer)
		self.contentPanel.SetAutoLayout(1)

		self.SetSizer(self.borderSizer)
		self.SetAutoLayout(1)

	def getThumbnailTmp(self,videocode):
		return urllib.urlretrieve("http://i.ytimg.com/vi/{0}/{1}.jpg".format(videocode,random.randint(1,3)))

	def updateText(self):
		title=""
		uploader=""
		newVal="""Title: {0}
uploader: {1}
""".format(title,uploader)
		self.text.ChangeValue(newVal)

class ScrollBox(scrolled.ScrolledPanel):
	def __init__(self, parent,size):
		scrolled.ScrolledPanel.__init__(self, parent, -1, size=size)

		self.vbox = wx.BoxSizer(wx.VERTICAL)

		self.SetSizer(self.vbox)
		self.SetAutoLayout(1)
		self.SetupScrolling()

	def addControl(self,control):
		# wx.ALL --> border(=borderwith) applied to all sides
		self.vbox.Add(control, 0, wx.EXPAND|wx.ALL, border=5)
		self.SetupScrolling()


class MainWindow(wx.Frame):

	numberOfVideos=0

	def __init__(self,parent,title):
		wx.Frame.__init__(self, parent, title=title, size=(600,400))

		# self.mainPanel = ScrolledPanel(self)
		self.mainPanel = ScrollBox(self, size=(560,-1))

		VideoPanel(self.mainPanel,"6ylq_DvYwB0",0,size=(0,0))
		# panel1 = VideoPanel(self.mainPanel,"6ylq_DvYwB0",0)
		# self.mainPanel.addControl(panel1)
		# panel2 = VideoPanel(self.mainPanel,"6ylq_DvYwB0",1)
		# self.mainPanel.addControl(panel2)
		# panel3 = VideoPanel(self.mainPanel,"6ylq_DvYwB0",2)
		# self.mainPanel.addControl(panel3)
		# panel4 = VideoPanel(self.mainPanel,"6ylq_DvYwB0",3)
		# self.mainPanel.addControl(panel4)
		# panel5 = VideoPanel(self.mainPanel,"6ylq_DvYwB0",3)
		# self.mainPanel.addControl(panel5)
		# panel6 = VideoPanel(self.mainPanel,"6ylq_DvYwB0",3)
		# self.mainPanel.addControl(panel6)
		# panel7 = VideoPanel(self.mainPanel,"6ylq_DvYwB0",3)
		# self.mainPanel.addControl(panel7)

		# self.mainPanel.SetupScrolling()
		self.Bind(wx.EVT_KEY_DOWN, self.onKeyPress)

	def onKeyPress(self,event):
		keycode = event.GetKeyCode()
		# print(keycode)
		# print(event.ControlDown)
		if keycode == 86 and event.ControlDown():
			self.addVideo()
		event.Skip()

	def addVideo(self):
		if not wx.TheClipboard.IsOpened():
			wx.TheClipboard.Open()
			dataObject = wx.TextDataObject()
			success = wx.TheClipboard.GetData(dataObject)
			value = dataObject.GetText()

			try:
				videoId = re.findall('(?:\?|&)v=([^&]*)',value)[0]
				panel = VideoPanel(self.mainPanel,videoId,0)
				self.mainPanel.addControl(panel)
				self.numberOfVideos+=1
			except Exception, e:
				print("Invalid URL")

			wx.TheClipboard.Close()
			print(value)
		else:
			print("Clipboard Error")


def init():
	print()

	app = wx.App(False)
	frame = MainWindow(None,"pyda")

	frame.Show()
	app.MainLoop()